//
//  GZPersonInfoController.swift
//  GZMarathon
//
//  Created by apple on 2021/10/27.
//

import UIKit
import AttributedString
import SVProgressHUD
import Kingfisher
class GZPersonInfoController: EBBaseViewController {

    
    @IBOutlet weak var tableView:
        UITableView!
        
    var alarmList: GZAlarmList?
    
    var info: GZPersonInfo?
       
    init(alarmList: GZAlarmList) {
        self.alarmList = alarmList
        super.init(nibName: nil, bundle: nil)
    }
    
    init(info: GZPersonInfo) {
        self.info = info
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        confTableView()
        if info == nil{
            getPersonInfo()
        }else{
            tableView.reloadData()
        }
    }
  
    
    func confTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: "GZPersonInfoCell", bundle: nil), forCellReuseIdentifier: "person_info")
        tableView.rowHeight = 650
        tableView.separatorStyle = .none
    }
    
    func getPersonInfo() {
        
        SVProgressHUD.show()
        ApiLoadingProvider.request(EBAPI.getPersonInfo(uid: alarmList!.parti_id ,name: alarmList!.parti_name), model: GZPersonInfo.self) {[weak self] model, isSuccess in
            
            SVProgressHUD.dismiss()
            guard let mm = model else{
                return
            }
            self?.info = mm
            self?.tableView.reloadData()
        }
    }


    @IBAction func clickBackBtn(_ sender: UIButton) {
        navigationController?.popViewController(animated: true)
    }
}

extension GZPersonInfoController: UITableViewDelegate, UITableViewDataSource{

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "person_info", for: indexPath) as! GZPersonInfoCell
        guard let personInfo = info else {
            return cell
        }
        
        let url = URL(string: personInfo.photo_url)
        cell.avatarImgView.kf.setImage(with: url)
        
        cell.nameLabel.text = personInfo.parti_name
        cell.sexLabel.attributed.text = getAttribute(str1: "性别:  ", str2: personInfo.parti_gender == "m" ? "男" : "女")
        
        cell.heightLabel.attributed.text = getAttribute(str1: "身高:  ", str2: personInfo.parti_stature)
        
        cell.ageLabel.attributed.text = getAttribute(str1: "年龄:  ", str2: personInfo.parti_age)
        
        cell.weightLabel.attributed.text = getAttribute(str1: "体重:  ", str2: personInfo.parti_weight)
        
        cell.fromLabel.attributed.text = getAttribute(str1: "籍贯:  ", str2: personInfo.parti_nativeplace)

        cell.telLabel.text = personInfo.parti_phone
        cell.contactNameLabel.text = personInfo.emergency_contact
        cell.contactTelLabel.text = personInfo.parti_phone
        cell.addressLabel.text = personInfo.parti_address
        cell.telLabel.text = personInfo.parti_phone
        cell.rateLabel.text = personInfo.parti_heartrate
        cell.codeImgView.image = getCodeImg(str: personInfo.health_code)
        return cell
        
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }

    func getAttribute(str1: String, str2: String) -> ASAttributedString {
        let color1 = UIColorFromRGB(0x999999)
        let color2 = UIColorFromRGB(0x333333)
        let font = UIFont(name: "PingFang SC", size: 14)
        
        let tr1: ASAttributedString = ASAttributedString(string: str1)
        let as1: ASAttributedString = .init(tr1, .font(font!), .foreground(color1))
        
        let tr2: ASAttributedString = ASAttributedString(string: str2)
        let as2: ASAttributedString = .init(tr2, .font(font!), .foreground(color2))
        
        return as1 + as2
        
    }
    
    func getCodeImg(str: String) -> UIImage?{
        let base64String = str.replacingOccurrences(of: "data:image/png;base64,", with: "")
                            
        //转换尝试判断，有可能返回的数据丢失"=="，如果丢失，swift校验不通过
        var imageData = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters)
        if imageData == nil {
            imageData = Data(base64Encoded: base64String + "==", options: .ignoreUnknownCharacters) //如果数据不正确，添加"=="重试
        }
        
        guard let data = imageData else{
            return nil
        }
        
        return UIImage(data: data)
    }

}
